﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmDichVuChiTiet : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler ButtonClicked = null;

        DmDichVuChiTietEntity dmDichVuChiTietEntity = null;
        DmDichVuChiTietBusiness dmDichVuChiTietBusiness = new DmDichVuChiTietBusiness();
        DmDichVuBusiness dmDichVuBusiness = new DmDichVuBusiness();

        int State = 0;

        public frmCapNhatDmDichVuChiTiet(int state)
        {
            InitializeComponent();
            this.State = state;
            LoadDichVu();
        }

        public frmCapNhatDmDichVuChiTiet(int state, DmDichVuChiTietEntity obj)
        {
            InitializeComponent();
            this.State = state;

            dmDichVuChiTietEntity = obj;
            LoadDichVu();

            lkDichVu.EditValue = dmDichVuChiTietEntity.MaDichVu;
            txtMaChiTieu.Text = dmDichVuChiTietEntity.MaChiTieu;
            txtTenChiTieu.Text = dmDichVuChiTietEntity.TenChiTieu;
            txtTriSoBinhThuong.Text = dmDichVuChiTietEntity.TriSoBinhThuong;
            txtDonVi.Text = dmDichVuChiTietEntity.DonVi;
            txtDonGia.Text = dmDichVuChiTietEntity.DonGia.ToString();
            txtSoThuTu.Text = dmDichVuChiTietEntity.SoThuTu.ToString();

            txtMaChiTieu.Enabled = false;
        }

        void LoadDichVu()
        {
            var lstDmDichVuEntity = dmDichVuBusiness.GetDmDichVuList();
            var dmDichVuEntity = new DmDichVuEntity()
            {
                MaDichVu = ConstantManagerClinic.sValueMember,
                TenDichVu = ConstantManagerClinic.sDisplayMember
            };
            lstDmDichVuEntity.Insert(0, dmDichVuEntity);

            lkDichVu.Properties.DataSource = lstDmDichVuEntity;
            lkDichVu.Properties.DisplayMember = "TenDichVu";
            lkDichVu.Properties.ValueMember = "MaDichVu";
            lkDichVu.EditValue = ConstantManagerClinic.sValueMember;

        }

        private void frmCapNhatDmDichVuChiTiet_Load(object sender, EventArgs e)
        {

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkDichVu, "dịch vụ") == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaChiTieu, "mã chỉ tiêu") == false) return;
            if (validateManager1.IsValid() == false) return;

            dmDichVuChiTietEntity = new DmDichVuChiTietEntity()
            {
                MaDichVu = lkDichVu.EditValue.ToString(),
                MaChiTieu = txtMaChiTieu.Text,
                TenChiTieu = txtTenChiTieu.Text,
                TriSoBinhThuong = txtTriSoBinhThuong.Text,
                DonVi = txtDonVi.Text,
                DonGia = Convert.ToDouble(txtDonGia.Text),
                SoThuTu = Convert.ToInt32(txtSoThuTu.Text),
                TrangThaiSuDung = chkTrangThaiSuDung.Checked
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmDichVuChiTietBusiness.GetDmDichVuChiTiet(txtMaChiTieu.Text) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaChiTieu, "mã chỉ tiêu") == false) return;
                }

                dmDichVuChiTietBusiness.InsertDmDichVuChiTiet(dmDichVuChiTietEntity);
                Validation.ClearControl(this);
                lkDichVu.Focus();
            }
            else
            {
                dmDichVuChiTietBusiness.EditDmDichVuChiTiet(dmDichVuChiTietEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (ButtonClicked != null) ButtonClicked(this, e);
        }

        private void frmCapNhatDmDichVuChiTiet_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                SendKeys.SendWait("{Tab}");
            }
        }
    }
}